import { Bank } from '../bank/bank-model.js';
import { My } from '../my/my-model.js';

import { Config } from '../config/config-model.js';
var bank = new Bank();
var config = new Config();
var my = new My();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //modal
    modalHidden: true,
    bankname: '',


    bankNumber: '',
    bank_user_name: '',
    accountIndex: 0,
    modalHidden: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that._loadData();
  },
  _loadData: function (options) {
    var that = this


  },
  //查询开户行名
  bankVal: function (e) {
    var bkVal = new Array();
    bkVal['bank_user_name'] = e.detail.value
    bank.selBankName(bkVal, (data) => {
      if (data == 1) {
        this.data.bankname = 1;
        this.data.errinfo = '卡号似乎不正确';

      } else {
        this.data.bankname = data;
        this.data.errinfo = '';
      }
      this.setData({
        bankName: this.data.bankname,
        bankNumber: e.detail.value.replace(/(^\s*)|(\s*$)/g, ""),
        errinfo: this.data.errinfo
      })
      console.log(data)

    })
  },


  //持卡人姓名
  bank_user_name: function (e) {
    this.setData({
      bank_user_name: e.detail.value.replace(/(^\s*)|(\s*$)/g, "")
    })
  },


  //添加银行卡
  submitBank: function (e) {

    var that = this
    if (this.data.bank_user_name == '' || this.data.bankNumber == '') {
      this.setData({
        errinfo: '请完善信息'
      })

    } else {
      this.setData({
        modalHidden: false,
      })

    }
  },
  //确认按钮
  success: function (e) {
    var bankArr = new Array();
    bankArr['bank_user_name'] = this.data.bank_user_name;
    bankArr['bank_number'] = this.data.bankNumber;
    bankArr['bank_name'] = this.data.bankname;
    bank.addBank(bankArr, (data) => {
      if (data) {
        wx.showToast({
          title: '已完成',
          icon: 'success',
          duration: 3000,
          success: function (res) {
            wx.navigateBack({
              url: '../withdrawals/withdrawals'

            })
          }
        });

      }
    })

  },

  //取消按钮
  cancel: function (e) {
    console.log(11)

    this.setData({
      modalHidden: true
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})